<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="depAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" @node-click="handleNodeClick">
          <template #default="{ data }">

            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width : 100%">
              <el-col :span="20">
                <svg-icon icon-class="home" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="depAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="depEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && !data.children.length" @click.native="depDelete(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

          </template>
        </el-tree>
      </el-card>
      <!-- 弹框 -->
      <el-dialog
        :title="addOrEdit?'编辑部门':'新增部门'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        @close="resetForm"
      >
        <template>
          <depDialog
            ref="reset"
            :pid="id"
            :add-or-edit="addOrEdit"
            :origin-list="originList"
            @closeDialog="showDialog=false"
            @depSuccess="reqSuccess"
          />
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { arr2tree } from '@/utils/index'
import depDialog from './deptDialog.vue'

export default {
  name: '',
  // 导入组件名
  components: { depDialog },
  // 父传子,接收变量名
  props: {},
  data() {
    // 这里存放数据
    return {
      // 树形结构的数据
      list: [],
      // 控制弹框显示隐藏的开关
      showDialog: false,
      // 存储子节点的父级id
      id: '',
      // 进行一个布尔值的存 判断当前点击的是否是编辑或者新增页面  true编辑 false新增
      addOrEdit: false,
      // 进行一个原始的部门列表数据的存 用来完成最后的校验
      originList: []
    }
  },
  // 计算属性 类似于 data 概念
  computed: {},
  // 监听器 监控 data 中的数据变化
  watch: {},
  created() {
    this.loadDepartment()
  },
  // 方法集合
  methods: {
    // 重置子组件表单
    resetForm() {
      this.$refs.reset.resetForm()
    },
    handleNodeClick(data) {
      console.log(data)
    },
    // 获取部门列表
    async loadDepartment() {
      const { data: res } = await getDepartment()
      // console.log(res)
      res.depts.shift()
      // 进行一个数据的过滤
      this.originList = res.depts.map(item => {
        return { code: item.code, id: item.id, pid: item.pid, name: item.name }
      })
      this.list = arr2tree(res.depts)
    },
    // 新增部门 传递给子组件id信息
    depAdd(id) {
      this.showDialog = true
      this.addOrEdit = false
      this.id = id
    },
    // 编辑部门
    depEdit(id) {
      this.showDialog = true
      this.addOrEdit = true
      this.id = id
      this.$nextTick(() => {
        this.$refs.reset.loadDepartmentInfo()
      })
    },
    // 关闭弹窗并渲染页面
    reqSuccess() {
      this.showDialog = false
      this.loadDepartment()
    },
    // 删除部门
    depDelete(id) {
      this.$confirm('您确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartment(id)
        console.log(res)
        // 重新渲染
        this.loadDepartment()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
</style>
